package edu.sdwu.liqing.qasystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import edu.sdwu.liqing.qasystem.entity.QaUser;
import edu.sdwu.liqing.qasystem.mapper.QaUserMapper;
import edu.sdwu.liqing.qasystem.service.IQaUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * 用户表  服务实现类
 * </p>
 *
 * @author liqing@sdwu.edu
 * @since 2020-03-22
 */
@Service
public class QaUserServiceImpl extends ServiceImpl<QaUserMapper, QaUser> implements IQaUserService {

    /**
     * login逻辑
     * 登录判断
     * 判断username对应的角色password是不是正确的
     * */
    @Override
    public boolean login(String username, String password) {
        if (StringUtils.isAnyBlank(username, password)) {
            return false;
        }
        QaUser user = getUserByUsername(username);
        if (Objects.isNull(user)) {
            //如果user为空，说明这个用户不存在，返回false
            return false;
        }
        if (StringUtils.equals(password, user.getPassword())) {
            return true;
        }
        return false;
    }

    /**
     * 根据username获取对应的那个用户实体
     * select * from qa_user where username = 'xxxxx'
     * */
    @Override
    public QaUser getUserByUsername(String username) {
        if (StringUtils.isBlank(username)) {
            return null;
        }
        return super.getBaseMapper().getUserDetailByUserName(username);
    }



}
